# -*- coding:UTF-8 -*- ＃
#!/usr/bin/python
'''
Created on 2013-5-4

@author: YANGCHENLONG
'''
import os.path,sys

Module = type(sys)
modules = {}

def load(fullpath, env={}, module=Module):
    try:
        code = open(fullpath).read()
    except IOError:
        raise ImportError, 'No Module named %s' %fullpath
    
    filename = os.path.basename(fullpath)
    
    try:
        return modules[filename]
    except KeyError:
        pass
    
    m = module(filename)
    m.__module_class__ = module
    m.__file__ = fullpath
    m.__dict__.update(env)
    
    exec compile(code, filename, 'exec') in m.__dict__
    modules[filename] = m
    
    return m

def unload(m):
    filename = os.path.basename(m.__file__)
    del modules[filename]
    return None

def reload(m):
    fullpath = m.__file__
    try:
        code = open(fullpath).read()
    except IOError:
        raise ImportError, 'No Module named %s' %fullpath
    
    env = m.__dict__
    module_class = m.__module_class__
    filename = os.path.basename(fullpath)
    m = module_class(filename)
    m.__file__ = fullpath
    m.__dict__.update(env)
    m.__module_class__ = module_class
    exec compile(code, filename, 'exec') in m.__dict__
    modules[filename] = m 
    
    return m
